<template>
  <div>
    <!-- 封装table :tree-props="tp" :row-key="rowKey" 这2个属性主要用户渲染tree数据时使用-->
    <el-table :data="data" border stripe>
      <el-table-column
        :label="item.title"
        :prop="item.name"
        :type="item.type"
        :width="item.width"
        :align="item.align"
        v-for="(item, index) in cols"
      >
        <!-- 定义作用域插槽,判断下cols里是否有定义插槽，否则会报错-->
        <template v-slot="{ row, index }" v-if="item.slot">
          <!-- 通过自定义属性名字传递作用域值过去 -->
          <slot :name="item.slot" :row1="row" :index1="index"> </slot>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :page-sizes="[1, 2, 5, 20]"
      @size-change="sizeChange"
      @current-change="currentChange"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 分页 -->
  </div>
</template>

<script>
export default {
  name: "MyTable",
  methods: {
    /**
     * 分页或页码更改事件
     */
    sizeChange(val) {
      console.log(49, val);
      this.$emit("size", val);
    },
    currentChange(val) {
      this.$emit("current", val);
    },
  },
  props: {
    tp: {
      type: Object,

      default: () => ({ hasChildren: "hasChildren", children: "children" }),
    },

    cols: {
      required: true,
      type: Array,
      default: () => [],
    },

    data: {
      required: true,
      type: Array,
      default: () => [],
    },

    total: Number,

    rowKey: {
      type: String,
      default: "id",
    },
  },
};
</script>

<style lang="scss" scoped>
.page {
  margin-top: 20px;
}

.table {
  margin-top: 20px;
}
</style>
